package com.tsystems.train.service;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import com.tsystems.train.dao.TrainDAO;
import com.tsystems.train.entity.Train;

/**
 * 
 * @author Turybrin Vladislav
 * 
 *         The class contains methods for performing basic operations with
 *         trains.
 */
@Stateless
public class TrainServices {

	@EJB
	private TrainDAO trainDAO;

	/**
	 * Get all trains from database.
	 * 
	 * @return trains
	 */
	public List<Train> viewTrains() {

		List<Train> trains = trainDAO.getTrainList();
		return trains;

	}

	/**
	 * Add new train if it has not already added
	 * 
	 * @param train
	 * 
	 * @return "New train added", if it so, otherwise - message with error
	 *         information
	 * 
	 */
	public String addTrain(Train train) {

		Train trainFound = trainDAO.getTrainByNumber(train.getNumber());

		if (trainFound != null) {
			return "This train already exist";
		}

		trainDAO.insert(train);
		return "New train added";
	}

}
